package com.wwm.makerandroid.makerandroid.View.Activity;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.google.gson.internal.LinkedTreeMap;
import com.lipiao.makerandroid.R;
import com.wwm.makerandroid.makerandroid.Bean.RespondBean.MessageBean;
import com.wwm.makerandroid.makerandroid.Bean.RespondBean.UserDto;
import com.wwm.makerandroid.makerandroid.MainActivity;
import com.wwm.makerandroid.makerandroid.SharedPreferences.SaveAccount;
import com.wwm.makerandroid.makerandroid.Utils.HttpUtil;

import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


//登陆
public class LoginActivity extends AppCompatActivity {
    static final String TAG = "LoginActivity";

    Map<String, String> userInfo;
    FloatingActionButton floatingActionButton;//登陆按钮
    static Context context;
    EditText etUsername;
    EditText etPassword;
    String userNumber;//账号
    String password;//密码
    String approvers;//用户请假的审批人id字符串
    private long lastClickBackTime = System.currentTimeMillis() - 3000;//三秒间隔 间隔内两次返回提示用户是否退出

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        context = getBaseContext();
        userInfo = SaveAccount.getUserInfo(this);//获取SharedPreferences中的数据
        // SharedPreferences 70行保存
        //初始化 全局网络请求工具类 时间戳格式化工具类  SQLite数据库工具类 均为单例
        initView();
        HttpUtil.getInstance();
    }

    private void initView() {
        floatingActionButton = findViewById(R.id.fb_login);
        etUsername = findViewById(R.id.et_user_number_login);
        etPassword = findViewById(R.id.et_password_login);
        etUsername.setText(userInfo.get("userNumber"));
        etPassword.setText(userInfo.get("password"));

    }

    //登录按钮 onclick属性对应的函数
    public void login(View view) {
        userNumber = etUsername.getText().toString().trim();
        password = etPassword.getText().toString().trim();
        //Log.d("LoginActivity", "login: " + userNumber + password);
        Call<MessageBean> call = HttpUtil.getUserService().login(userNumber, password);
        call.enqueue(new Callback<MessageBean>() {
            @Override
            public void onResponse(Call<MessageBean> call, Response<MessageBean> response) {
                LinkedTreeMap strBack = (LinkedTreeMap) response.body().getData();
//                fixme strBack 为linkTreeMap 转为 hashMap
                //获取到响应
                if ( response.body().isSuccess()) {
                    Toast.makeText(context, "登录成功!", Toast.LENGTH_LONG).show();
                    SaveAccount.saveAccountInfo(context, userNumber, password);//保存账号密码
                    //将数据转到MainActivity
                    Intent intent = new Intent(context, MainActivity.class);
                    intent.putExtra("userNumber", userNumber);
                    intent.putExtra("password", password);
                    intent.putExtra("approvers", approvers);

                    LinkedTreeMap linkedTreeMap= (LinkedTreeMap) strBack.get("user");
                    intent.putExtra("role",String.valueOf(Integer.parseInt(linkedTreeMap.get("role").toString().substring(0,1))));
                    intent.putExtra("name",String.valueOf((linkedTreeMap.get("name"))));
                    startActivity(intent);
                    finish();
                } else {
                    Toast.makeText(context, "登录失败!" + strBack, Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onFailure(Call<MessageBean> call, Throwable t) {
                Toast.makeText(context, t.getMessage(), Toast.LENGTH_LONG).show();
                Log.d(TAG, "onResponse: error");
                Log.d(TAG, t.getMessage());
            }
        });


    }

    //注册按钮 onclick属性对应的函数
    public void register(View view) {
        startActivity(new Intent(context, RegisterActivity.class));
    }

    //退出提醒 两次back退出
    @Override
    public void onBackPressed() {
        if (System.currentTimeMillis() - lastClickBackTime > 2000) { // 后退阻断
            Toast.makeText(this, "再点击退出 Android博客阅读app", Toast.LENGTH_LONG).show();
            lastClickBackTime = System.currentTimeMillis();
        } else { // 关掉app
            System.exit(0);//完全退出  再次启动很慢
//            finish();//保留进程 再次启动较快 但有不确定的bug没解决
        }
    }
    //跳过登录按钮
    public void skip(View view){
        Intent intent = new Intent(context, MainActivity.class);
        startActivity(intent);
    }

}